/* Copyright 2009 Palm, Inc.  All rights reserved. */

function AppAssistant(appController){
    this.appController = appController;
	this.emergencyNumbers = ["911"]; // default
	this.populateEmergencyNumbers();
}

function StageAssistant(stageController){
    this.stageController = stageController;
    Mojo.Controller.appController.mainController = stageController;

	var appAssistant = Mojo.Controller.getAppController().assistant;
	appAssistant.handleLaunchType(appAssistant.launchParams);
}

AppAssistant.prototype.handleLaunch = function(params){
	this.launchParams = params;
	if (Mojo.Controller.appController.mainController) {
		Mojo.Controller.appController.mainController.activate();
		this.handleLaunchType(params);
	}
};

AppAssistant.prototype.populateEmergencyNumbers = function() {
	var request = new Mojo.Service.Request("palm://com.palm.telephony", {
        method: 'emergencycalllistQuery',
        onSuccess: function(payload) {
			if (payload && payload.extended && payload.extended.value) {
	            this.emergencyNumbers = payload.extended.value.match(/[\*\#0-9]+/g);
			}
		}.bind(this),
		onFailure: function(e) {
			 Mojo.Log.error("palm://com.palm.telephony/emergencycalllistQuery failed with error %j",e);
		},
		onComplete: function() {
			request = undefined;
		}
    });
};

AppAssistant.prototype.handleLaunchType = function(params){
    var launchParams = {};
    var stageController = Mojo.Controller.appController.mainController;
    try {
        launchParams = PalmSystem.launchParams.evalJSON(true);
    } 
    catch (err) {
        Mojo.Log.info("PhonePrefs: No launch params");
    }
    
    
    var args = launchParams;
    Mojo.Log.info("Phone Prefs Launch Params:" + Object.toJSON(args));
    if (args) {
        if (args.launchType) {
			if(args.callingapp && args.callingapp === "firstuse") {
				if(args.locale && args.locale.length === 5 && args.locale != Mojo.Locale.current) {
					Mojo.Locale.set(args.locale);
				}
			}
            switch (args.launchType) {
                case "devicelock":
                    if (stageController) {
                        this.deviceLock(stageController, args.type, args.unblock);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.deviceLock.bind(this, args.type, args.unblock, args.callingapp));
                    }
				break;
                case "unlockTelephony":
                    if (stageController) {
                        this.unlockTelephony(stageController, args.callingapp);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.unlockTelephony.bind(this, args.callingapp));
                    }
				break;
                case "puk1Lock":
                    if (stageController) {
                        this.pin1Reset(stageController, args.callingapp);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.pin1Reset.bind(this, args.callingapp));
                    }
				break;
                case "fdnDisable":
                    if (stageController) {
                        this.fdnDisableOrPUK2(stageController, args.callingapp, args.number);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.fdnDisableOrPUK2.bind(this, args.callingapp, args.number));
                    }
				break;
                case "puk2Lock":
                    if (stageController) {
                        this.pin2Reset(stageController, args.callingapp);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.pin2Reset.bind(this, args.callingapp));
                    }
                break;
				case "startNetworkSearch": 
					if (stageController) {
                        this.startNetworkSearch(stageController);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.startNetworkSearch.bind(this));
                    }
				break;
				case "apn": 
					if (stageController) {
                        this.setApn(stageController, args.callingapp);
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.setApn.bind(this, args.callingapp));
                    }
				break;
				case "mms": 
					if (stageController) {
                        this.setApn(stageController, args.callingapp, "mms");
                    }
                    else {
                        Mojo.Controller.appController.createStageWithCallback("PhonePrefs", this.setApn.bind(this, args.callingapp, "mms"));
                    }
				break;
				default:
                    break;
            }
        }
        else {
			// if we're already open, just focus.  prevents pushing on top of
			// an existing scene
			if (stageController.topScene()) {
				stageController.activate();
			} else {
				stageController.pushScene("preflist");
			}
            
        }
    }
    
};

AppAssistant.prototype.setApn = function(stageController, callingapp, category){
	stageController.activate();
	var args={type:"setApn", 'callingapp':callingapp, 'category':category};
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch', args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		args.closeapp = true;
		stageController.pushScene("preflist", args);
	}
};

AppAssistant.prototype.unlockTelephony = function(stageController, callingapp){
	stageController.activate();
	var args={type:"unlockTelephony", 'callingapp':callingapp};
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch', args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		args.closeapp = true;
		stageController.pushScene("preflist", args);
	}
};


AppAssistant.prototype.pin1Reset = function(stageController, callingapp){
	stageController.activate();
	var args={type:"pin1Reset", 'callingapp':callingapp};
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch',args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		args.closeapp = true;
		stageController.pushScene("preflist", args);
	}
};

AppAssistant.prototype.pin2Reset = function(stageController, callingapp, number){
	stageController.activate();
	var args;
	if(number !== undefined) {
		args={type:"pin2Reset",'callingapp':callingapp, 'number':number};
	} else {
		args={type:"pin2Reset",'callingapp':callingapp};
	}
	
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch',args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		args.closeapp = true;
		stageController.pushScene("preflist", args);
	}
};

AppAssistant.prototype.fdnDisableOrPUK2 = function(stageController, callingapp, number){
	stageController.activate();
	this.pin2StatusReq = TelephonyService.pin2Status(this.fdnDisableOrpin2Reset.bind(this, stageController, callingapp, number));
};
AppAssistant.prototype.fdnDisableOrpin2Reset = function(stageController, callingapp, number, payload){
	stageController.activate();
	if (payload && payload.returnValue){
		if (payload.extended.pukrequired){
			this.pin2Reset(stageController, callingapp, number);
		}
		else{
			this.fdnDisable(stageController, callingapp, number);
		}
	}
	else{
		this.fdnDisable(stageController, callingapp, number);
	}
};

AppAssistant.prototype.fdnDisable = function(stageController, callingapp, number){
	stageController.activate();
	var args={'type':"fdnDisable",'callingapp':callingapp,'number':number};
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch',args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		args.closeapp = true;
		stageController.pushScene("preflist", args);
	}
};

AppAssistant.prototype.startNetworkSearch = function(stageController){
	stageController.activate();
		var args={type:"startNetworkSearch"};
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch', args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		stageController.pushScene("preflist", args);
	}
};

AppAssistant.prototype.deviceLock = function(stageController, type, unblock, callingapp){
	stageController.activate();
	var args={'type':"deviceLock",'locktype':type,'unblock':unblock,'callingapp':callingapp};
	if (stageController.topScene() && stageController.topScene().sceneName == "preflist") {
		stageController.delegateToSceneAssistant('handleExternalLaunch',args);
	} else if(stageController.topScene()){
		stageController.popScenesTo("preflist",args);
	}
	else {
		args.closeapp = true;
		stageController.pushScene("preflist", args);
	}
};